<template>
    <div class="content-container">
        <el-card>
            <!-- 查询部分 -->
            <el-form :model="query" :inline="true" class="demo-form-inline">
                <el-form-item>
                    <el-button type="primary" @click="openDialog(null)">新增</el-button>

                    <el-popconfirm confirm-button-text="确认" cancel-button-text="取消" title="确认删除选中数据?"
                        @confirm="removeRows" width="200px">
                        <template #reference>
                            <el-button type="danger">删除</el-button>
                        </template>
                    </el-popconfirm>
                </el-form-item>
            </el-form>
            <!-- 数据部分 -->
            <el-table max-height="600px" :data="tableData" ref="tableDataRef" stripe row-key="id" show-overflow-tooltip
                default-expand-all style="width: 100%; margin-top: 10px; max-height: 500px; overflow: auto">
                <el-table-column type="selection" />
                <el-table-column prop="name" label="部门名称" />
                <el-table-column prop="createTime" label="创建时间" />
                <el-table-column prop="updateTime" label="修改时间" />
                <el-table-column fixed="right" label="操作" width="120">
                    <template #default="scope">
                        <el-button @click="openDialog(scope.row)" link type="primary">
                            编辑
                        </el-button>
                        <el-button @click="viewDialog(scope.row)" link type="success">
                            查看
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-dialog v-model="dialogVisible" :title="(formData.id ? '编辑' : '新增') + '部门信息'" width="60%"
                :show-close="false" :close-on-click-modal="false">
                <el-form :rules="rules" :model="formData" class="demo-form-inline" ref="formDataRef">
                    <el-form-item prop="name" label="部门名称">
                        <el-input v-model="formData.name" clearable />
                    </el-form-item>
                    <el-form-item prop="parentId" label="上级部门">
                        <el-tree-select v-model="formData.parentId" :data="treeList" :render-after-expand="false"
                            :props="{ label: 'name', value: 'id' }" clearable searchable filterable check-strictly
                            accordion />
                    </el-form-item>
                </el-form>
                <template #footer>
                    <span class="dialog-footer">
                        <el-button @click="dialogVisible = false">取消</el-button>
                        <el-button type="primary" @click="formSubmit">确认</el-button>
                    </span>
                </template>
            </el-dialog>

            <el-dialog v-model="viewVisible" width="60%" :show-close="false" :close-on-click-modal="false">
                <el-input v-model="viewData.name" disabled>
                    <template #prepend>部门名称</template>
                </el-input>
                <el-input v-model="viewData.createTime" disabled>
                    <template #prepend>创建时间</template>
                </el-input>
                <el-input v-model="viewData.updateTime" disabled>
                    <template #prepend>修改时间</template>
                </el-input>
                <template #footer>
                    <span class="dialog-footer">
                        <el-button @click="viewVisible = false">关闭</el-button>
                    </span>
                </template>
            </el-dialog>
        </el-card>
    </div>
</template>
<script setup lang="ts" name="Dept">
import { onMounted, ref, reactive, toRefs } from 'vue'
import type { Dept, DeptModDTO, DeptQueryDTO, DeptVo } from '@/api/system/dept/type'
import { deptAdd, deptDel, deptMod, deptTree } from '@/api/system/dept'
import { ElNotification } from 'element-plus'

const formDataRef = ref()
const tableDataRef = ref()
const initQuery = () => {
    return { current: 1, size: 10 } as DeptQueryDTO
}
const state = reactive<any>({
    query: initQuery(),
    tableData: [] as Dept[],
    total: 0,
    dialogVisible: false,
    viewVisible: false,
    viewData: {} as DeptVo,
    formData: {} as DeptModDTO,
    treeList: [] as Dept[],
    rules: {
        name: [{ required: true, message: '部门名称不能为空', trigger: 'blur' }],
        parentId: [
            { required: true, message: '上级部门不能为空', trigger: 'change' },
        ],
    },
})
const {
    query,
    tableData,
    dialogVisible,
    formData,
    rules,
    viewVisible,
    viewData,
    treeList,
} = toRefs(state)
const openDialog = (data: any) => {
    if (formDataRef.value) {
        formDataRef.value.resetFields()
    }

    formData.value = data == null ? {} : data
    dialogVisible.value = true
}
const viewDialog = (data: any) => {
    viewVisible.value = true
    viewData.value = data
}
onMounted(() => {
    buildTable()
})
const buildTable = async () => {
    const result = await deptTree()
    tableData.value = result.data
    treeList.value = [{ id: '0', name: '顶级部门', children: result.data }]
}

const formSubmit = async () => {
    await formDataRef.value?.validate()
    if (formData.value.id) {
        await deptMod(formData.value)
    } else {
        await deptAdd(formData.value)
    }
    dialogVisible.value = false
    await buildTable()
}
const removeRows = async () => {
    const rows = tableDataRef.value.getSelectionRows()
    if (rows.length == 0) {
        ElNotification({
            title: '删除失败',
            message: '请选择要删除的数据',
            type: 'warning',
        })
        return
    }
    const ids = rows.map((each: any) => each.id)
    await deptDel({ ids: ids })
    await buildTable()
}
</script>

<style scoped lang="scss">
.content-container {
    padding: 12px;
}
</style>